package com.heima.wemedia.service.impl;

import com.heima.apis.article.IArticleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.CommentRepaysDtos;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.pojos.ApCommentRepays;
import com.heima.model.wemedia.vo.ApCommentVo;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.pojos.ApComment;
import com.heima.model.wemedia.dtos.CommetStatusDtos;
import com.heima.wemedia.service.WmCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WmCommentServiceImpl implements WmCommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //注入远程调用
    @Autowired
    private IArticleClient iArticleClient;

    //打开或关闭评论
    @Override
    public ResponseResult updateCommentStatus(CommetStatusDtos dto) {
        //校验数据
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断登录
        WmUser wmUser = WmThreadLocalUtil.getWmUser();
        if (wmUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        /*ApComment commenr = new ApComment();
        commenr.setEntryId(dto.getArticleId());
        commenr.setFlag(dto.getOperation());*/
        Query query = new Query(Criteria.where("entryId").is(dto.getArticleId()));
        ApComment com = mongoTemplate.findOne(query, ApComment.class);

        //远程调用feign接口，修改ap_article_config
        ResponseResult responseResult = iArticleClient.updateArticleConfig(dto);
        if (responseResult.getCode()!=200){
            return ResponseResult.errorResult(500,"失败");
        }

        //不存在数据就不要往下走了
        if (com == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //逻辑删除
        Update update = Update.update("flag", dto.getOperation());
        mongoTemplate.updateMulti(query, update, ApComment.class);

        //删除所有评论
        //mongoTemplate.remove(query,ApComment.class);
        //删除评论回复
        //Query query1 = new Query(Criteria.where("entryId").is(commenr.getEntryId()));

        return ResponseResult.okResult(200, "成功");
    }

    //文章评论详情
    @Override
    public ResponseResult details(CommentRepaysDtos dto) {

        //校验数据
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断登录
        WmUser wmUser = WmThreadLocalUtil.getWmUser();
        if (wmUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //校验页码和大小
        int page = dto.getPage() <= 0 || dto.getPage() == null ? 1 : dto.getPage();
        int size = dto.getSize() > 10 || dto.getSize() == null ? 10 : dto.getSize();

        Query query = new Query(Criteria.where("entryId").is(dto.getArticleId()));


        //分页查询
        List<ApComment> apComments = mongoTemplate.find(query.skip((page - 1) * size).limit(size), ApComment.class);

        List<ApCommentVo> apCommentVos = new ArrayList<>();

        //List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);

        for (ApComment apComment : apComments) {
            Query query1 = new Query(Criteria.where("commentId").is(apComment.getId()));
            List<ApCommentRepays> apCommentRepays = mongoTemplate.find(query1, ApCommentRepays.class);
            ApCommentVo apCommentVo = new ApCommentVo();
            apCommentVo.setApCommentRepays(apCommentRepays);
            com.heima.model.wemedia.pojos.ApComment apComment1 = new com.heima.model.wemedia.pojos.ApComment();
            BeanUtils.copyProperties(apComment,apComment1);
            apCommentVo.setApComments(apComment1);
            apCommentVos.add(apCommentVo);
        }
        return ResponseResult.okResult(apCommentVos);
    }
}
